package com.piggy.settlement.sdk.client.v2.singlewithdraw;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.piggy.settlement.sdk.constant.v2.PiggySingleWithdrawalV2Constants;
import com.piggy.settlement.sdk.util.v2.PiggyOpenBasicsVerifyUtil;

/**
 * @author ：daxia
 * @description：单笔提现相关接口V2
 * @modified By：
 * @version: 1.0$
 */
public class PiggySingleWithdrawalApiV2Client {

    static PiggySingleWithdrawalApiV2Client piggySingleWithdrawalApiV2Client = null;

    private String appSecret = new String();
    private String domain = new String();

    public PiggySingleWithdrawalApiV2Client(String appSecret, String domain) {
        this.appSecret = appSecret;
        this.domain = domain;
    }

    public static PiggySingleWithdrawalApiV2Client getInstanse(String appSecret, String domain) {
        if (piggySingleWithdrawalApiV2Client == null) {
            piggySingleWithdrawalApiV2Client =
                    new PiggySingleWithdrawalApiV2Client(appSecret, domain);
        }

        return piggySingleWithdrawalApiV2Client;
    }

    /**
     * 单笔提现上报
     *
     * @param signleVo 参数实体：
     * @return
     */
    public JSONObject singleSubmit(Object signleVo) {

        try {
            String signStr = PiggyOpenBasicsVerifyUtil.getSigStr(appSecret, signleVo);

            JSONObject paramObj = JSONUtil.parseObj(signleVo);
            paramObj.putOpt("sign", signStr);

            String response = HttpRequest.post(domain + PiggySingleWithdrawalV2Constants.SINGLE_SUBMIT)
                    .body(paramObj.toString())
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("V2-单笔提现上报失败:");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 单笔提现上报特殊
     *
     * @param signleVo 参数实体：
     * @return
     */
    public JSONObject singleSubmitLimit(Object signleVo) {

        try {
            String signStr = PiggyOpenBasicsVerifyUtil.getSigStr(appSecret, signleVo);

            JSONObject paramObj = JSONUtil.parseObj(signleVo);
            paramObj.putOpt("sign", signStr);

            String response = HttpRequest.post(domain + PiggySingleWithdrawalV2Constants.SINGLE_SUBMIT_LIMIT)
                    .body(paramObj.toString())
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("V2-特殊单笔提现上报失败:");
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 单笔提现上报结果查询
     *
     * @param signleVo 参数实体：
     * @return
     */
    public JSONObject querySingleTrade(Object signleVo) {

        try {
            String signStr = PiggyOpenBasicsVerifyUtil.getSigStr(appSecret, signleVo);

            JSONObject paramObj = JSONUtil.parseObj(signleVo);
            paramObj.putOpt("sign", signStr);

            String response = HttpRequest.post(domain + PiggySingleWithdrawalV2Constants.QUERY_SINGLE_TRADE)
                    .body(paramObj.toString())
                    .timeout(20000)
                    .execute()
                    .body();

            JSONObject object = JSONUtil.parseObj(response);
            return object;
        } catch (Exception e) {
            System.out.println("V2-单笔提现上报结果失败:");
            e.printStackTrace();
            return null;
        }
    }

}
